package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.maincardmanager;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard.CardReplaceFragment;
import com.zzsy.carosshopprojects.activitys.home.mainpage.common.scan.ScanAc;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.MessageEvent;
import com.zzsy.carosshopprojects.view.dialog.GoUpPopupDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 实体卡管理
 */
public class MainCardManagerFragment extends BaseFragment {
    public static MainCardManagerFragment newInstance() {
        MainCardManagerFragment mainCardManagerFragment = new MainCardManagerFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("couponList", "");
        mainCardManagerFragment.setArguments(bundle);
        return mainCardManagerFragment;
    }

    private String[] permissionArrs = {Manifest.permission.CAMERA};

    @Override
    protected int initLayout() {
        return R.layout.fg_maincardmanager;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.MainCardManager), getActivity());
    }

    @Override
    protected void initData(Context mContext) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void loadMethod() {

    }

    @OnClick({R.id.img_bu_card, R.id.img_xiao_card, R.id.img_del_card})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_bu_card:  //实体卡补卡
                start(CardReplaceFragment.newInstance("", "1"));
                break;
            case R.id.img_xiao_card://解绑实体卡
                GoUpPopupDialog.newInstance(0, false, GoUpPopupDialog.CARDREPLACE).show(getFragmentManager(), "dialog");
                break;
            case R.id.img_del_card://删除红包
                Scanner(13);
                break;
        }
    }

    /**
     * 扫一扫功能
     */
    public void Scanner(int num) {
        if (EasyPermissions.hasPermissions(getContext(), permissionArrs)) {
            ScanAc.start(getActivity(), num);
            getActivity().finish();
        } else {
            EasyPermissions.requestPermissions(getActivity(), getContext().getResources().getString(R.string.get_photo_permission_tip), 0x11, permissionArrs);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        switch (event.message) {
            case MessageEvent.REPLACEMENTCARDS:
                startWithPop(PinCardFragment.newInstance(event.content));
                break;
        }
    }


}
